const CategoryService = require("../service/category.service")

class CategoryController {
    async create(ctx,next) {
        const { name } = ctx.request.body
        
        const result = await CategoryService.create(name)

        ctx.body = result
    }
    async list(ctx,next) {
        const page = ctx.query.page || 1
        const pageSize = ctx.query.pageSize || 10
        const {totalCount} = await CategoryService.getListCount()
        const offset = (page - 1) * pageSize

        const items = await CategoryService.getList(pageSize,offset)

        ctx.body = {
            totalCount,
            items
        }
    }
    async edit(ctx,next) {
        const { id,name } = ctx.request.body
        const result = await CategoryService.edit(id,name)
        ctx.body = result
    }
    async remove(ctx,next) {
        const { id } = ctx.query
        const result = await CategoryService.remove(id)
        ctx.body = result
    }
}

module.exports = new CategoryController()